package com.spzx.order.reveiver;
/*
    并非全力
*/

import com.rabbitmq.client.Channel;
import com.spzx.rabbit.constant.MqConst;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @RabbitListener 注解用于定义一个方法来监听 RabbitMQ 的消息队列
 *
 * bindings 属性用于指定与队列的绑定关系
 *
 * @QueueBinding 注解定义了队列的绑定细节
 *
 * exchange 属性指定了交换机的配置
 *   - value 属性设置了交换机的名称为 MqConst.EXCHANGE_TEST
 *   - durable 属性设置为 "true"，表示交换机是持久化的，即在服务器重启后仍然存在
 *
 * value 属性指定了队列的配置
 *   - value 属性设置了队列的名称为 MqConst.QUEUE_TEST
 *   - durable 属性设置为 "true"，表示队列是持久化的，即在服务器重启后仍然存在
 *
 * key 属性指定了路由键，这里为 MqConst.ROUTING_TEST，表示消息通过此路由键从交换机路由到该队列
 */
@Component
public class TestReceiver {
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_TEST,durable = "true"),
            key = {MqConst.ROUTING_TEST}
    ))
    public void a(Channel channel, Message message, String msg) throws Exception{
        byte[] body = message.getBody();
        String bodyStr = new String(body);
        System.out.println("接收到消息：" + bodyStr);
        System.out.println(msg);
        //默认自动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
